package com.ts.api.module.channel.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.file.FileUtils;
import com.gitee.apanlh.util.io.FileIOUtils;
import com.gitee.apanlh.util.log.Log;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.WebFile;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.context.ChannelUserContext;
import com.ts.api.config.UploadPathConfig;
import com.ts.api.exp.BizException;
import com.ts.api.module.channel.entity.po.ChannelComplaintPO;
import com.ts.api.module.channel.entity.vo.ReqChannelComplaintVO;
import com.ts.api.module.channel.service.ChannelComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 *  渠道意见反馈
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/channel/complaint")
public class ChannelComplaintController {

    @Autowired
    private ChannelComplaintService channelComplaintService;
    @Autowired
    private UploadPathConfig uploadPathConfig;

    /**
     *  插入意见反馈
     *
     *  @param  requestVO
     *  @return ResultVO
     */
    @PostMapping("/insert")
    public ResultVO<Object> insert(@RequestParamVo RequestVO requestVO) {
        ReqChannelComplaintVO complaintVO = requestVO.toBean(ReqChannelComplaintVO.class);
        Assert.isNotNullThrows(complaintVO, new BizException(BizEnum.EMPTY));

        //  需要修改
        //  文件上传
        WebFile webFile = requestVO.getWebFile();
        String filePath = null;
        if (ValidParam.isNotNull(webFile)) {
            MultipartFile file = webFile.getFile();

            if (ValidParam.isNotNull(file)) {
                try {
                    byte[] bytes = file.getBytes();
                    String extendOfPhoto = FileUtils.getExtendOfPhoto(bytes);
                    if (ValidParam.isEmpty(extendOfPhoto)) {
                        throw new BizException(BizEnum.UPLOAD_IMG_SUFFIX_ERROR);
                    }
                    filePath = uploadPathConfig.getCommonPath() + webFile.createUniqueFileName(file);
                    //  添加尾部
                    if (FileUtils.getSuffix(filePath) == null) {
                        filePath = StringUtils.append(true, filePath, extendOfPhoto);
                    }
                    FileIOUtils.write(bytes, FileIOUtils.getOutput(filePath));
                } catch (Exception e) {
                    if (e instanceof BizException) {
                        throw (BizException) e;
                    }
                    Log.get().info("{}", e.getMessage(), e);
                    throw new BizException(BizEnum.UPLOAD_ERROR);
                }
            }
        }

        ChannelComplaintPO complaintPO = CopyUtils.copy(complaintVO, ChannelComplaintPO.class);
        complaintPO.setType(ClassConvertUtils.toInt(complaintVO.getType()));
        complaintPO.setLoanChannelId(ChannelContext.getId());
        complaintPO.setLoanUserId(ChannelUserContext.getId());
        complaintPO.setImgPath(filePath);
        complaintPO.setCreateTime(DateUtils.currentTime());
        channelComplaintService.insert(complaintPO);
        return ResultVO.suc();
    }
}
